package com.aceitunaproject.gameclient;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


import com.aceitunaproject.mapdisplay.resources.ImageLibrary;
import com.aceitunaproject.shared.gamelement.Installation;
import com.aceitunaproject.shared.gamelement.Settlement;
import com.aceitunaproject.shared.gamelement.Unit;
import com.aceitunaproject.shared.map.MapTile;

/**
 * Panel to show details about a tile
 * 
 * @author Federico Bergstein
 * 
 */
public class DetailsPanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;

	private AceitunaView view;

	Map<String, Unit> tileUnits;
	Map<String, Installation> tileInstallations;
	Settlement tileSettlement;

	public DetailsPanel(AceitunaView view) {
		this.view = view;
		this.setBorder(BorderFactory.createEtchedBorder());
	}

	private JButton createButton(Unit element) {
		tileUnits.put(element.getId().toString(), element);

		return createButton(Utility.getUnitImageName(element), element.getName(), "U:"
				+ element.getId().toString());
	}

	private JButton createButton(Installation element) {
		tileInstallations.put(element.getId().toString(), element);

		

		return createButton(Utility.getInstallationImageName(element), element.getName(), "I:"
				+ element.getId().toString());
	}

	private JButton createButton(Settlement element) {

		tileSettlement = element;
		return createButton(Utility.getSettlementImageName(element), element.getName(), "S:"
				+ element.getId().toString());
	}

	private JButton createButton(String imageName, String text, String id) {
		Image image = ImageLibrary.getImage(imageName);

		Icon icon = new ImageIcon(image);
		JButton button = new JButton(text, icon);
		button.setActionCommand(id.toString());

		button.addActionListener(this);
		return button;
	}

	public void setTile(MapTile mapTile) {

		this.removeAll();
		
		tileUnits = new HashMap<String, Unit>();
		tileInstallations = new HashMap<String, Installation>();
		tileSettlement = null;
		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		Image image = ImageLibrary.getBasicTileImage(mapTile.getTerrain()
				.get(0).getName(), mapTile.getPosx(), mapTile.getPosy());

		Icon icon = new ImageIcon(image.getScaledInstance(32, 32,
				Image.SCALE_DEFAULT));
		
		this.add(new JLabel(mapTile.getTerrain().get(0).getName(), icon,
				JLabel.RIGHT));

		for (Unit unit : mapTile.getUnits()) {

			this.add(createButton(unit));

		}
		for (Installation installation : mapTile.getInstallations()) {

			this.add(createButton(installation));

		}

		Settlement theSettlement = mapTile.getSettlement();
		if (theSettlement != null) {
			this.add(createButton(theSettlement));
		}

		this.revalidate();
		this.repaint();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		String[] a = arg0.getActionCommand().split(":");

		if(a[0].equals("U"))
			view.unitSelected(tileUnits.get(a[1]));

		if(a[0].equals("I"))
			view.installationSelected(tileInstallations.get(a[1]));

		if(a[0].equals("S"))
			view.settlementSelected(tileSettlement);

	

	}

}
